package com.example.h.dao;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by H on 2017/12/12.
 */

public class DbHelper extends SQLiteOpenHelper{

    private static final String DATABASE_NAME="mydb.db";
    private static final int VERSION=6;

    private DbHelper(Context context){
        super(context,DATABASE_NAME,null,VERSION);
    }
    public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="create table if not exists User(" +
                "u_id integer primary key,"+
                "u_name text,"+
                "u_password"+
                ")";
        db.execSQL(sql);
        Log.i("creat Db","创建用户表成功");
        sql="insert into User(u_name,u_password) values('lucy','123456')";
        db.execSQL(sql);
        Log.i("insert Db","插入数据成功");
        sql="create table if not exists UserInfo("+
                "i_id integer primary key,"+
                "u_id integer,"+
                "i_sex text,"+
                "i_age int,"+
                "i_address text"+
                ")";
        db.execSQL(sql);
        Log.i("creat Db","创建用户信息表成功");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private static DbHelper dbHelper;

    public static DbHelper getInstance(Context context) {
        if(dbHelper==null)
            dbHelper=new DbHelper(context);
        return dbHelper;
    }
}
